
// pages/login/login.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */

  data:{
    nickName:"",
    openId:""
  },

  onLoad(options) {
    wx.login({
      success: (res) => {
        let code = res.code;
        console.log(code);
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session',
          method:'GET',
          data:{
            appid: 'wx746c775244f127e3',
            js_code: code,
            secret: 'dfc5d685d7bf2c91a2349c9fd88ec927',
            grant_type: 'authorization_code'
          },
          success: (res) =>{
            openId:res.data.openid
          }
        })
      },
    })
    
    wx.request({
      url:'http://119.91.29.88:1111/user/selectByOpenId',
      data:{
        openId:openId
      },
      method: "GET",
      success(res){
        console.log(res.data);
        // 将返回的user放入全局变量
        app.globalData.cur_user=res.data[0];
        //跳转到主页
        wx.redirectTo({
          url: '/pages/index/index',
          })
      },
      fail(res){
      }
    })
  },

  loginSys:function(){
    let that=this;
    let user = {
      nickName:that.data.nickName,
      createTime:new Date(),
      openId:openId
    }
    //hhhhhhhh
    wx.request({
      url: 'http://119.91.29.88:1111/user/save',
      data: user,
      success(res){
        //保存当前用户到全局作用域
        console.log(user);
        app.globalData.cur_user=user;
        //跳转到主页
        wx.redirectTo({
        url: '/pages/index/index',
        })
      }, 
      method:"POST"
    })
  }
})